//package cn.good.yan.b4.a2;
//
//import com.alibaba.fastjson.JSONObject;
//import com.crm.common.core.domain.R;
//import com.crm.sm.common.annotation.ModifyChangeRecordHistory;
//import com.crm.sm.service.design.AopStrategy;
//import com.crm.sm.service.design.history.impl.*;
//import org.apache.http.HttpStatus;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.UnsupportedEncodingException;
//import java.lang.reflect.Method;
//import java.util.Enumeration;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Objects;
//
//import static com.crm.sm.common.enums.ChangeRecHisTableEnum.getByCode;
//
///**
// * pss 切面 （修改） - AOP （需要引AOP包）
// *
// * @author shengren.yan
// * @create 2022/9/19
// */
//@Aspect
//@Component
//public class PssModifyAop {
//
//    protected final Logger log = LoggerFactory.getLogger(this.getClass());
//
//    private ThreadLocal<Object> target = new ThreadLocal<>();
//
//    /**
//     * 定义切点
//     */
//    @Pointcut("@annotation(com.crm.sm.common.annotation.ModifyChangeRecordHistory)")
//    public void pssDealPssAop() {
//    }
//
//    /**
//     * 通知前
//     *
//     * @param joinPoint 连接点
//     */
//    @Before("pssDealPssAop()")
//    public void doBefore(JoinPoint joinPoint) {
//        try {
//            // ... 逻辑
//        } catch (Exception e) {
//            log.info("【PssModifyAop】切面-拦截器前-出现异常-请求参数" + JSONObject.toJSONString(target.get()));
//        }
//    }
//
//    /**
//     * 通知后
//     *
//     * @param joinPoint 连接点
//     * @param result    结果
//     * @throws UnsupportedEncodingException 不支持编码异常
//     */
//    @AfterReturning(pointcut = "pssDealPssAop()", returning = "result")
//    public void doAfterReturning(JoinPoint joinPoint, R<String> result) throws Exception {
//        try {
//            // 状态异常 - 不进行操作
//            if (null != result && result.getCode() != HttpStatus.SC_OK) {
//                return;
//            }
//            // ... 逻辑
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.info("【PssModifyAop】切面-拦截器后--出现异常--请求参数" + JSONObject.toJSONString(target.get()));
//        }
//        target.remove();
//    }
//
//    /**
//     * 通知异常
//     *
//     * @param joinPoint 点
//     * @param ex        前女友
//     * @throws Exception 异常
//     */
//    @AfterThrowing(pointcut = "pssDealPssAop()", throwing = "ex")
//    public void addAfterThrowingLogger(JoinPoint joinPoint, Exception ex) throws Exception {
//        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if (attributes != null) {
//            HttpServletRequest request = attributes.getRequest();
//            Enumeration headerNames = request.getHeaderNames();
//            Map<String, String> map = new HashMap<>(16);
//            while (headerNames.hasMoreElements()) {
//                String key = (String) headerNames.nextElement();
//                String value = request.getHeader(key);
//                map.put(key, value);
//            }
//            log.info("【PssModifyAop】切面--异常具体信息---" + ex.getMessage() + " 请求消息" + map);
//        }
//    }
//
//    /**
//     * 反射获取参数
//     *
//     * @param joinPoint 连接点
//     * @return {@code String}
//     * @throws Exception 异常
//     */
//    private String getTableName(JoinPoint joinPoint) throws Exception {
//        Class<?> clazz = joinPoint.getTarget().getClass();
//        Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getMethod().getParameterTypes();
//        Method method = clazz.getMethod(joinPoint.getSignature().getName(), parameterTypes);
//        ModifyChangeRecordHistory changeRecordHistory = method.getAnnotation(ModifyChangeRecordHistory.class);
//        return changeRecordHistory.type().getIndex();
//    }
//
//}
